/// 
/// MaDAson - Toolkit for Model Driven Software Development
/// 
/// Copyright [2006] Marcus Lankenau
/// 
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///  
///     http://www.apache.org/licenses/LICENSE-2.0
///  
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// 
using System;
using System.IO;
using Madason.MetaModel;
using Madason.MetaModel.Maven;
using System.Collections.Generic;
using Madason.MetaModel.Interfaces;
using System.Xml;
using Madason.MetaModel.Validation;
using System.Reflection;
using Madason.GenUtils;

/// <summary>
/// Summary description for Program
/// </summary>
public class Program
{
	private Madason.MetaModel.Model model;


	/// <summary>
	/// the current model
	/// </summary>
	public Madason.MetaModel.Model Model
	{
		get { return model; }
		set { model = value; }
	}



	/// <summary>
	/// generate maven directories and pom xml files
	/// </summary>
	/// <param name="classes"></param>
	/// <param name="argMap"></param>
	public static void GenerateMavenProject(MavenProject project, Dictionary<string, string> argMap, DirectoryInfo dir)
	{
		using (SensorWriter pomWriter = new SensorWriter(dir.FullName + "\\pom.xml"))
		{
			ProtectedSections sections = new ProtectedSections();
			FileInfo pomFileInfo = new FileInfo(dir.FullName + "\\pom.xml");
			if (pomFileInfo.Exists)
				sections.Load(pomFileInfo);
		
			Pom pomTemplate = new Pom();
			pomTemplate.Project = project;
			pomTemplate.ProtectedSection = sections;
			pomTemplate.Render(pomWriter);
			pomWriter.Close();

			project.GenerateDirectoryStructure(dir);


			foreach (MavenProject childProject in project.SubProjects)
			{
				DirectoryInfo childDir = dir.CreateSubdirectory(childProject.ArtifactId);
				GenerateMavenProject(childProject, argMap, childDir);
			}
		}
	}

	/// <summary>
	/// main function
	/// - parse command argumends
	/// - invoke special generator-functions
	/// </summary>
	/// <param name="args"></param>
	[STAThread]
	public static int Main(string[] args)
	{
		GeneratorWorkflow workflow = new GeneratorWorkflow(args);

		workflow.AddParameterDefinition("package", ParameterType.Text, "Root package of your projecstructure", true);
		workflow.CheckCommandLine();
		workflow.LoadModel();
		workflow.Transform();
		workflow.Validate();


		//
		// Generate sources
		//
		string initialNamespace = workflow.GetArg("package");
		Namespace ns = workflow.Model.FindByName(initialNamespace);
		MavenProject project = ns as MavenProject;
		if (project == null)
		{
			Console.WriteLine("Can't find project at package: " + initialNamespace);
			if (ns != null)
				Console.WriteLine("{0} is of stereotype {1}", ns.FullName, ns.StereotypeName);
			return -1;
		}

		GenerateMavenProject(project, workflow.Arguments, new DirectoryInfo("."));


		return 0;
	}
}
